import { BASE_URL } from '@/utils/ip.js'

export default {
  methods: {
    /**
     * 获取附件.之前的名称
     */
    getFileTypeName(src){
      if(src) {
        let newSrc = src.split('.')[1]
        let list = ['bmp','jpg','jpeg','png','gif','xls','xlsx','doc','docx','pdf',
                      'html','txt','xml','htm','ppt','pptx','rar','zip','gz','aac','bz2',
                      'flv','avi','mp4','rmvb','wmv','mpg','asf','m3u8','3gp','mp3','wav',
                      'wma','mid','m4a','swf','rm','dwg']
        if( !list.includes(newSrc)){
          newSrc ='other'
        }
        return newSrc
      }
    },
    /**
     * 附件下载
     */
    download (src) {
      let arr = [] , id = src
      if(src.indexOf('.') > -1){
        arr = src.split('.')[0].split('/')
        id = arr[arr.length - 1]
      }
      uni.showLoading({ title: '下载中...' })
      uni.downloadFile({
        url: BASE_URL + '/system/archives/download/' + id,
        success: res => {
          if (res.statusCode === 200) {
            // #ifndef H5
            uni.saveFile({
              tempFilePath: res.tempFilePath,
              success: red => {
                
                uni.showToast({ title: '下载成功', icon: 'none' })
                uni.showModal({
                  title: '提示',
                  content: '文件已保存在：' + res.tempFilePath,
                  cancelText: '我知道了',
                  confirmText: '打开文件',
                  success: (res) => {
                    if (res.confirm) {
                      uni.openDocument({
                        filePath: red.savedFilePath,
                        showMenu: true,
                        success: r => {
                          this.$refs.uToast.show({ title: '文件打开中...' })
                        },
                        fail: error => {
                          this.$refs.uToast.show({ title: '文件打开失败', type: 'error' })
                        }
                      })
                    }
                  }
                });
              }
            })
            // #endif
          }
        },
        complete: () => {
          uni.hideLoading();
        }
      })
    },

    getFileType (src) {
      const type = src.split('.')[1]
      return this.$getFileClass(type)
    },
    getSrc (src) {
      return BASE_URL + '/system/archives/preview/thumb/' + src
    },
    /**
     * 查看大图
     */
    imgPreview (data, index) {
      let list = []
      if(data.length){
        let list = data.map(item => {
          return this.getSrc(item)
        })
      }else{
        list = [this.getSrc(data)]
      }
      uni.previewImage({
        current: index,
        urls: list,
        fail: () => {
          uni.showToast({
            title: '预览图片失败',
            icon: 'none'
          })
        }
      })
    }
  }
}
